// components/shop/index.js
const app = getApp()


Component({
  /**
   * 组件的属性列表
   */
  properties: {
    model: { // 属性名
      type: Object,
      value: {}
    },

  },
  data: {
    viewModel: {},
    collect: false
  },
  observers: {
    'model': function (model) {
      // 在 numberA 或者 numberB 被设置时，执行这个函数
      console.info(model);
      if (model != null) {
        this.setData({
          "collect": model.collect
        });
        if (app.lat) {
          var dis = app.getDistance(app.lat, app.lng, model.lat, model.lng);
          console.info(dis);
          if (dis > 1) {

            if (dis > 1000) {
              dis = dis / 1000;
              dis = dis.toFixed(0);
              dis = dis + "km"
            } else {
              if(dis.toFixed){
                dis = dis.toFixed(0) + "m"
              }else{
                dis = dis+ "m"
              }
             
            }
            dis = "距离您" + dis;
            this.setData({
              distance: dis
            });
          }

        }

      }
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    goShop() {
      var model = this.properties.model;
      wx.navigateTo({
        url: '/pages/main/shop/index?id=' + model.id,
      })
    },
    navToShop() {
      let self = this;
      var model = this.properties.model;

      wx.openLocation({
        latitude: model.lat,
        longitude: model.lng,
        name: model.name,
        address: model.address,
        scale: 18
      })
    },
    collectShop() {
      var model = this.properties.model;
      var param = {};
      param.shop = model.id;
      var func = this.backCreateData.bind(this);
      app.postRequest("/rest/shopcollect/create.htm", param, func);

    },
    backCreateData(res) {
      if (res.code == 0) {
        var model = this.properties.model;
        model.collectId = res.id;
        this.setData({
          "collect": true
        });
        app.showModal("关注成功");

      } else {
        app.showModal(res.msg);
      }
    },
    cancelCollectShop() {
      var model = this.properties.model;
      var param = {};
      param.id = model.collectId;
      var func = this.backDeleteData.bind(this);

      app.postRequest("/rest/shopcollect/delete.htm", param, func);
    },
    backDeleteData(res) {
      if (res.code == 0) {
        app.showModal("取消成功");
        this.setData({
          "collect": false
        });

      } else {
        app.showModal(res.msg);
      }
    }
  }
})